package com.rui.study.algorithm.L_深入理解JVM_类加载的时机_不会引发_父类静态字段间接访问;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2019-03-05 09:36
 **/
public class ClientClassLoader extends ClassLoader {

    public static final String PATH = "D:/0_yaowr/temp/client/";

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        InputStream in;
        String filename = PATH + name + ".class";
        try {
            in = new FileInputStream(filename);
        } catch (FileNotFoundException e) {
            throw new ClassNotFoundException("类找不到[" + filename + "]");
        }

        byte[] buf;
        try {
            buf = new byte[in.available()];
            in.read(buf);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }

        return defineClass(name , buf, 0, buf.length);
    }

}
